// pages/userExercises/exam/exam.js
import {
  request,
  getRootPath
} from '../../../utils/util.js';

Page({

  /**
   * 页面的初始数据
   */
  data: {
    show_title: false,
    show_title_time: 10, //显示标题的时间
    show_tip: false,
    show_tip_time: 5, //显示标题的时间
    show_answer_time: 3, //答题后，展示答案时间

    answer_index: null, //用户选择的选项角标
    right_index: null, //正确答案角标

    is_over: false, //是否答题结束

    examList: [],
    exam_index: 0,

    error_num: 0, //错误数量
    right_num: 0, //正确数量

    time: 10, //倒计时
    init_time: 10, //初始化倒计时时间
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.audioCtx = wx.createInnerAudioContext();
    this.data.section_id = options.id;
    this.getExamList();
  },

  //获取题目列表
  getExamList: function(is_again = 0) { //is_again 是否再测一次
    let that = this;
    request({
      url: 'Example/getExample',
      data: {
        id: that.data.section_id,
        is_again: is_again,
      },
      method: 'POST',
      success: data => {
        this.setData({
          examList: data,
          answer_index: null,
          right_index: null,
          show_title: false,
          show_tip: false,
          exam_index: 0,
          is_over: false,
          right_num: 0,
          error_num: 0
        })
        wx.showModal({
          title: '温馨提示',
          content: '请做好答题准备',
          showCancel: true,
          cancelText: '没准备好',
          confirmText: '准备好了',
          success: function(res) {
            if (res.confirm) {
              that.playAudio(2);
              that.start();
            } else {
              that.back();
            }
          },
        })
      },
    })
  },

  start: function() {
    this.interval = setInterval(() => {
      let {
        time,
        exam_index,
        examList,
        answer_index,
        right_num,
        right_index,
        error_num,
        show_title_time,
        show_tip_time,
        show_title,
        show_tip
      } = this.data;

      time--;
      if (time == -1) {
        exam_index++;

        if (answer_index == undefined) {
          error_num++;
        }

        if (exam_index > examList.length - 1) {
          clearInterval(this.interval)
          exam_index--;
          this.setData({
            is_over: true,
            time: this.data.init_time
          })
        } else {
          answer_index = null;
          right_index = null;
        }
        time = this.data.init_time;
        this.playAudio(2);
        show_tip = false;
        show_title = false;
        this.answer();
      }

      if (time <= show_title_time) {
        show_title = true;
      }

      if (time <= show_tip_time) {
        show_tip = true;
      }

      this.setData({
        time,
        exam_index,
        right_num,
        error_num,
        answer_index,
        right_index,
        show_title,
        show_tip
      })
    }, 1000)
  },

  onUnload: function() {
    clearInterval(this.interval)
  },

  //判断回答是否正确
  checkAnswer: function() {
    let {
      examList,
      exam_index,
      error_num,
      right_num,
      right_index,
      answer_index,
      show_answer_time
    } = this.data;

    //判断是否回答正确
    if (answer_index != undefined && right_index != undefined) {
      if (answer_index == right_index) {
        right_num++;
        this.playAudio(1);
      } else {
        error_num++;
        this.playAudio(0);
      }
    } else {
      error_num++;
      this.playAudio(0);
    }

    this.setData({
      error_num,
      right_num
    })
  },

  //选择答案
  selectAnswer: function(e) {
    let index = e.currentTarget.dataset.index;
    let {
      examList,
      exam_index,
      error_num,
      right_num,
      answer_index,
      show_answer_time
    } = this.data;

    if (answer_index != undefined) {
      return;
    } else {
      answer_index = index;
    }

    let right_index = null;
    let right_option = examList[exam_index].right_option;
    for (let i = 0; i < examList[exam_index].options.length; i++) {
      if (examList[exam_index].options[i] == right_option) {
        right_index = i;
        break;
      }
    }

    this.setData({
      answer_index,
      right_index,
      right_num,
      error_num,
      show_title: true,
      show_tip: true,
      time: show_answer_time
    })

    //判断是否回答正确
    this.checkAnswer();
    this.answer();
  },

  //下一题
  next: function() {
    let {
      examList,
      exam_index,
      answer_index,
      right_index,
      error_num,
      right_num,
      init_time
    } = this.data;

    //判断是否正确
    if (answer_index == undefined && right_index == undefined) {
      error_num++;
    }

    exam_index++;
    if (exam_index > examList.length - 1) {
      clearInterval(this.interval)
      this.setData({
        is_over: true,
        right_num,
        error_num,
        time: init_time,
      })
    } else {
      this.setData({
        exam_index,
        answer_index: null,
        right_index: null,
        show_tip: false,
        show_title: false,
        right_num,
        error_num,
        time: init_time,
      })
      this.playAudio(2);
    }
    this.answer();
  },

  //再测一次
  again: function() {
    clearInterval(this.interval);
    this.getExamList(1);
  },

  //返回
  back: function() {
    wx.navigateBack()
  },

  //提交答案
  answer: function() {
    let that = this;
    let {
      examList,
      exam_index,
      answer_index,
    } = this.data;

    let id = examList[exam_index].id;
    let answer = examList[exam_index].options[answer_index] || "";
    clearInterval(this.interval);
    request({
      url: 'Example/subAnswer',
      data: {
        id,
        answer
      },
      method: 'POST',
      success: data => {
        if (!this.data.is_over) {
          this.start();
        }
      }
    })
  },

  //播放音效
  playAudio: function(type = 0) { //type=0：播放答错音效，type=1：播放答对音效，type=2：播放单词音效
    this.audioCtx.stop();
    let {
      examList,
      exam_index,
    } = this.data;
    if (type == 0) {
      this.audioCtx.src = '/res/audio/error.mp3';
    } else if (type == 1) {
      this.audioCtx.src = '/res/audio/right.mp3';
    } else {
      this.audioCtx.src = getRootPath(examList[exam_index].audio_path);
    }
    this.audioCtx.play();
  },

  //再听一次
  listen: function() {
    this.playAudio(2)
  }
})